from flask import Blueprint,jsonify,request
from app.service.studentservice import StudentService
from app.mapper.database import StudInfo

stud_bp = Blueprint('stud',__name__)
studentService = StudentService()

# 查询
@stud_bp.route('/selectall',methods=['GET'])
def selectAll():
    # userInfo = userService.select_all_users()
    page = int(request.args.get('page'))
    # print(page)
    # print(type(page))
    json_data = studentService.select_all_stud(page)

    return jsonify(json_data)


# 添加学生
@stud_bp.route('/addstud',methods=['POST'])
def add_stud():
    data = request.get_json()
    studno = data.get('studno')
    studname= data.get('studname')
    studsex = data.get('studsex')
    email = data.get('email')
    stud = StudInfo(studname,studsex,email)
    stud.set_studno(studno)
    print(stud.to_json())

    res = studentService.add_stud(stud)

    return jsonify(res)

# 删除学生
@stud_bp.route('/deletestud',methods=['GET'])
def delete_stud():
    userid = int(request.args.get('userid'))
    res = studentService.deelete_by_id(userid)

    return jsonify(res)

# 修改学生
@stud_bp.route('/editstud',methods=['POST'])
def edit_stud():
    data = request.get_json()
   
    studname = data.get('studname')
    studsex = data.get('studsex')
    email = data.get('email')

    stud = StudInfo(studname,studsex,email)
    
    res = studentService.edit_stud(stud)

    return jsonify(res)

# 通过姓名查找
@stud_bp.route('/selectbyname',methods=['GET'])
def select_by_name():
    studname = request.args.get('studname')

    res = studentService.select_by_studname(studname)

    return jsonify(res)